<?php
/**
 * This is index scaffold template. 
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil.views.scaffolds
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
?>
<div class="head">
<h1><?php echo $pluralHumanName;?></h1>
</div>

<div class="<?php echo $pluralVar;?> index">
<table cellpadding="0" cellspacing="0">
<tr>
<?php foreach ($scaffoldFields as $_field):?>
	<th><?php echo $this->Paginator->sort($_field);?></th>
<?php endforeach;?>
	<th><?php __d('lil', 'Actions');?></th>
</tr>
<?php
$i = 0;
foreach (${$pluralVar} as ${$singularVar}):
	$class = null;
	if ($i++ % 2 == 0) {
		$class = ' class="altrow"';
	}
echo "\n";
	echo "\t<tr{$class}>\n";
		foreach ($scaffoldFields as $_field) {
			$isKey = false;
			if (!empty($associations['belongsTo'])) {
				foreach ($associations['belongsTo'] as $_alias => $_details) {
					if ($_field === $_details['foreignKey']) {
						$isKey = true;
						echo "\t\t<td>\n\t\t\t" . $this->Html->link(${$singularVar}[$_alias][$_details['displayField']], array('controller' => $_details['controller'], 'action' => 'view', ${$singularVar}[$_alias][$_details['primaryKey']])) . "\n\t\t</td>\n";
						break;
					}
				}
			}
			if ($isKey !== true) {
				echo "\t\t<td>\n\t\t\t" . ${$singularVar}[$modelClass][$_field] . " \n\t\t</td>\n";
			}
		}

		echo "\t\t<td class=\"actions\">\n";
		echo "\t\t\t" . $this->Html->link(__d('lil', 'View', true), array('action' => 'view', ${$singularVar}[$modelClass][$primaryKey])) . "\n";
		echo "\t\t\t" . $this->Html->link(__d('lil', 'Edit', true), array('action' => 'edit', ${$singularVar}[$modelClass][$primaryKey])) . "\n";
		echo "\t\t\t" . $this->Html->link(__d('lil', 'Delete', true), array('action' => 'delete', ${$singularVar}[$modelClass][$primaryKey]), null, __d('lil', 'Are you sure you want to delete', true).' #' . ${$singularVar}[$modelClass][$primaryKey]) . "\n";
		echo "\t\t</td>\n";
	echo "\t</tr>\n";

endforeach;
echo "\n";
?>
<tfoot>
	<tr>
		<td colspan="<?php echo sizeof($scaffoldFields)+1; ?>">
		<?php
			echo $this->Paginator->prev('<< '.__d('lil', 'previous', true), array('class'=>'prev'), null, array('class'=>'prev light'));
			echo $this->Paginator->next(__d('lil', 'next', true).' >>', array('class'=>'next'), null, array('class'=>'next light'));
			echo '<div class="counter">';
			echo $this->Paginator->counter(array('format' => __d('lil', 'Page <b>%page%</b> of <b>%pages%</b>, total <b>%count%</b> records.', true)));
			echo '</div>';
		?>
		</td>
	</tr>
</tfoot>
</table>
</div>

<div class="actions">
	<div><?php echo $this->Html->link(sprintf(__d('lil', 'New %s', true), $singularHumanName), array('action' => 'add')); ?></div>
<?php
		$done = array();
		foreach ($associations as $_type => $_data) {
			foreach ($_data as $_alias => $_details) {
				if ($_details['controller'] != $this->name && !in_array($_details['controller'], $done)) {
					echo "\t<div>" . $this->Html->link(sprintf(__d('lil', 'List %s', true), Inflector::humanize($_details['controller'])), array('controller' => $_details['controller'], 'action' => 'index')) . "</div>\n";
					echo "\t<div>" . $this->Html->link(sprintf(__d('lil', 'New %s', true), Inflector::humanize(Inflector::underscore($_alias))), array('controller' => $_details['controller'], 'action' => 'add')) . "</div>\n";
					$done[] = $_details['controller'];
				}
			}
		}
?>
</div>